package org.poker.swing;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.poker.Poker;
import org.poker.exception.PokerException;
		

public class Actions extends JFrame {
	
	private static final long serialVersionUID = 7746611687894508858L;

	protected final Action actionMarcarDesmarcarCarta;
	protected final Action actionTrocarCartasMarcadas;
	protected final Action actionDistribuirNovasCartas;
	
	protected EventControl eventControl;
	protected KeyControl keyControl;
	
	
	public Actions(String titulo) {
		
		super(titulo);
		
		this.actionMarcarDesmarcarCarta = new AbstractAction("MarcarDesmarcarCarta")
		{
			private static final long serialVersionUID = -4164312478235804984L;

			public void actionPerformed(ActionEvent event)
			{
				System.out.println("-- marcar carta...");
				try {
					int id = -1;
					String action = event.getActionCommand();
					JButton button = (JButton)event.getSource();
					if(action.length()>6 && action.substring(0, 6).equals("carta_")){
						id = Integer.valueOf(action.substring(6, 7)).intValue()-1;
					} else if(button.getName().length()>7 && button.getName().substring(0, 7).equals("button_")){
						id = Integer.valueOf(button.getName().substring(7, 8)).intValue();
					}
					Poker.selectUnselectCard(id); // seleciona / deseleciona uma carta
					JButton[] buttonsSuperior = (JButton[])this.getValue("buttonsSuperior");
					for(int i=0; i<buttonsSuperior.length; i++) {
						buttonsSuperior[i].setBackground(Poker.idSelectedHandCard(i)?Color.LIGHT_GRAY:Color.WHITE);
					}
				} catch (PokerException e) {
					e.printStackTrace();
				}
			}		
		};

		this.actionTrocarCartasMarcadas = new AbstractAction("TrocarCartasMarcadas")
		{
			private static final long serialVersionUID = 7037886501698918762L;

			public void actionPerformed(ActionEvent event)
			{
				System.out.println("-- trocar cartas...");
				
				JButton[] buttonsSuperior = (JButton[])this.getValue("buttonsSuperior");
				JButton[] buttonsInferior = (JButton[])this.getValue("buttonsInferior");
				JLabel[] labelsInferior = (JLabel[])this.getValue("labelsInferior");
				buttonsInferior[1].setEnabled(true);
				buttonsInferior[0].setEnabled(false);
				buttonsInferior[0].transferFocus();
				
				try {
					Poker.switchSelectedCards(); // troca cartas marcadas
					for(int i=0; i< 5; i++) {
						buttonsSuperior[i].setEnabled(false);
						buttonsSuperior[i].setBackground(Color.WHITE);
						buttonsSuperior[i].setText(Poker.getHandCard(i));
						buttonsSuperior[i].setForeground(Poker.isRedCardInHand(i)?Color.RED:Color.BLACK);
					}
				} catch (PokerException e) {
					e.printStackTrace();
				}
				labelsInferior[0].setText(Poker.printPoints());
				labelsInferior[1].setText(Poker.printResultLabel());
				labelsInferior[1].transferFocus();
				
			}		
		};
		
		this.actionDistribuirNovasCartas = new AbstractAction("DistribuirNovasCartas") 
		{
			private static final long serialVersionUID = -826225029817504168L;

			public void actionPerformed(ActionEvent event)
			{
				System.out.println("-- distribuir cartas...");
				
				JButton[] buttonsSuperior;
				Frame frame;
				try {
					Poker.newDeck(); // novo baralho
					Poker.newCards(); // 5 novas cartas
					
					buttonsSuperior = (JButton[])this.getValue("buttonsSuperior");
					JButton[] buttonsInferior = (JButton[])this.getValue("buttonsInferior");
					JLabel[] labelsInferior = (JLabel[])this.getValue("labelsInferior");
					frame = (Frame)this.getValue("frame");
					buttonsInferior[1].setEnabled(false);
					buttonsInferior[0].setEnabled(true);
					labelsInferior[0].setText(Poker.printPoints());
//					labelsInferior[1].setText(Poker.printResultLabel());
					labelsInferior[1].setText("");
					for(int i=0; i< 5; i++) {
						buttonsSuperior[i].setEnabled(true);
						buttonsSuperior[i].setBackground(Color.WHITE);
						buttonsSuperior[i].setText(Poker.getHandCard(i));
						buttonsSuperior[i].setForeground(Poker.isRedCardInHand(i)?Color.RED:Color.BLACK);
						buttonsSuperior[1].transferFocus();
						this.putValue("frame", frame);
					}
				} catch (PokerException e) {
					e.printStackTrace();
				}
			}
		};
		
		this.keyControl = new KeyControl(new Action[] {Actions.this.actionMarcarDesmarcarCarta, 
														   Actions.this.actionTrocarCartasMarcadas, 
														   Actions.this.actionDistribuirNovasCartas});
	}
}
